package com.fingard.dsp.bank.directbank.bocom13;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.directbank.bocom13.util.JavaMD5;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.TcpPortClient;
import com.fingard.text.StringHelper;
import com.fingard.util.GZipHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.*;

/**
 * 交通银行资金清算平台(BOCOM13)批量代收付
 */
public class BOCOM13BatPayRec extends BOCOM13Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return d_BatPayRec(p_req9188);
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return d_QryBatPayRec(p_req9198);
    }

    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        retAts.reqSeqID = p_reqBat.reqBatchNO;

        TcpPortClient tmpClient = null;
        FileOutputStream fos = null;
        try {
            String tmpPayDate = Format.Date8Format.format(p_reqBat.getPayDateTime());

            String bankBranch = getBankConfig().getMapValueByKey("bankBranch", p_reqBat.ownAct);
            if (StringHelper.isNullOrEmpty(bankBranch)) {
                retAts.respCode = "0";
                retAts.respInfo = "未配置账号[" + p_reqBat.ownAct + "]银行方分支机构映射,请检查config.xml中'bankBranch'配置项!";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            String fileType = "";
            if (p_reqBat.reqHeader.isBatRec) {
                fileType = BOCOM13TransCode.batRec_9188_918801;
            } else {
                fileType = BOCOM13TransCode.batPay_1988_198801;
            }

            String tmpBatSeqNo = getBatSeqNo(getTempFileDirectory(), 5);
            String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_reqBat.ownAct);
            if (StringHelper.isNullOrEmpty(corpNo)) {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqBat.ownAct + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置";
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = FailCode.E9981;
                retAts.writeRetToATS();
                return retAts;
            }

            String tmpUploadFileName = "I_B_" + tmpPayDate + "_" + corpNo + "_" + bankBranch + "_" + fileType + ".S." + tmpBatSeqNo;
            String tmpLocalFilePath = createTempFileDirectory() + File.separator + tmpUploadFileName;
            File tmpLocalFile = new File(tmpLocalFilePath);
            if (!tmpLocalFile.exists()) {
                tmpLocalFile.createNewFile();
            }
            WriteBankLogLn2("待压缩批次请求文件保存本机路径:\r\n" + tmpLocalFilePath);
            WriteBankLogLn2("待压缩批次请求文件开始写入数据...");

            // 1、拼接批量代收付文件内容
            byte tmpByteSp = 0x0F;// 域分隔符
            byte tmpByteLn = (byte) '\n';// 换行符

            fos = new FileOutputStream(tmpLocalFilePath);
            fos.write("3012900".getBytes(getCharset()));/* 银行方ID:(3012900:交通银行) */
            fos.write(tmpByteSp);

            fos.write(bankBranch.getBytes(getCharset()));/* 银行分行代码:保险公司发起方账号开户分行代码 */
            fos.write(tmpByteSp);

            fos.write(tmpByteSp);/* 应答类型 */
            fos.write(tmpByteSp);/* 错误码 */
            fos.write(tmpByteSp);/* 错误说明 */
            fos.write(tmpByteSp);/* 银行方业务日期 */
            fos.write(tmpByteSp);/* 银行方交易日期 */
            fos.write(tmpByteSp);/* 银行方交易时间 */
            fos.write(tmpByteSp);/* 银行方业务日期 */
            fos.write(tmpByteSp);/* 银行方交易日期 */
            fos.write(tmpByteSp);/* 银行方交易时间 */
            fos.write(tmpByteSp);/* 银行方批次号 */

            fos.write(corpNo.getBytes(getCharset()));/*保险方ID: 保险公司编码 */
            fos.write(tmpByteSp);

            fos.write(tmpPayDate.getBytes(getCharset()));/* 保险方业务日期(yyyyMMdd) */
            fos.write(tmpByteSp);

            fos.write(tmpPayDate.getBytes(getCharset()));/* 保险方交易日期(yyyyMMdd) */
            fos.write(tmpByteSp);

            fos.write(Format.Time6Format.format(p_reqBat.getPayDateTime()).getBytes(getCharset()));/* 保险方交易时间 */
            fos.write(tmpByteSp);

            fos.write(p_reqBat.reqBatchNO.getBytes(getCharset()));/* 保险方批次号 */
            fos.write(tmpByteSp);

            fos.write(p_reqBat.totalNum.getBytes(getCharset()));/* 总笔数 */
            fos.write(tmpByteSp);

            fos.write(p_reqBat.totalAmt.getBytes(getCharset()));/* 总金额 */
            fos.write(tmpByteSp);

            fos.write(tmpByteSp);/* 成功笔数 */
            fos.write(tmpByteSp);/* 成功金额 */
            fos.write(tmpByteSp);/* 预留字段01 */
            fos.write(tmpByteSp);/* 预留字段02 */
            fos.write(tmpByteSp);/* 预留字段03 */
            fos.write(tmpByteSp);/* 预留字段05 */
            fos.write(tmpByteSp);/* 预留字段06 */
            fos.write(tmpByteSp);/* 预留字段07 */
            fos.write(tmpByteSp);/* 预留字段08 */
            fos.write(tmpByteSp);/* 预留字段09 */
            fos.write(tmpByteSp);/* 预留字段10 */

            int tmpRdCount = 0;
            while (p_reqBat.readRecord()) {
                ++tmpRdCount;
                fos.write(tmpByteLn);
                fos.write(String.valueOf(tmpRdCount).getBytes(getCharset()));/* 记录序号 */
                fos.write(tmpByteSp);

                fos.write(tmpByteSp);/* 应答类型 */
                fos.write(tmpByteSp);/* 错误码 */
                fos.write(tmpByteSp);/* 错误说明 */
                fos.write(tmpByteSp);/* 银行方业务日期 */
                fos.write(tmpByteSp);/* 银行方流水号 */

                fos.write(tmpPayDate.getBytes(getCharset()));/* 保险方业务日期 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.askNumber.getBytes(getCharset()));/* 保险方流水号 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.insuPolicyNum.getBytes(getCharset()));/* 投保单号 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.sourceNoteCode.getBytes(getCharset()));/* 保单号 */
                fos.write(tmpByteSp);

                String oppName = "";
                if (p_reqBat.reqHeader.isBatRec) {
                    oppName = p_reqBat.readedDetail.payName;
                } else {
                    oppName = p_reqBat.readedDetail.recName;
                }
                fos.write(oppName.getBytes(getCharset()));/* 客户姓名 */
                fos.write(tmpByteSp);

                fos.write(convertCertTypeToBank(p_reqBat.readedDetail.certType).getBytes(getCharset()));/* 证件类型 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.certNum.getBytes(getCharset()));/* 证件号码 */
                fos.write(tmpByteSp);

                String isPrivate = "";
                if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
                    isPrivate = "P";
                } else {
                    isPrivate = "E";
                }
                fos.write(isPrivate.getBytes(getCharset()));/* 公私标志(E-对公,P-对私) */
                fos.write(tmpByteSp);

                String sameBnk = "";
                if ("1".equals(p_reqBat.readedDetail.sameBnk)) {
                    sameBnk = "1";
                } else {
                    sameBnk = "2";
                }
                fos.write(sameBnk.getBytes(getCharset()));/* 银行标志(1-交通银行 2-非交通银行) */
                fos.write(tmpByteSp);

                String cnapsCode = "";
                if ("0".equals(p_reqBat.readedDetail.sameBnk)) {
                    cnapsCode = p_reqBat.readedDetail.cnapsCode;
                }
                fos.write(cnapsCode.getBytes(getCharset()));/* 收款行行号 */
                fos.write(tmpByteSp);

                fos.write(tmpByteSp);/* 险种名称 */

                String oppAct = "";
                if (p_reqBat.reqHeader.isBatRec) {
                    oppAct = p_reqBat.readedDetail.payAct;
                } else {
                    oppAct = p_reqBat.readedDetail.recAct;
                }
                fos.write(oppAct.getBytes(getCharset()));/* 账号 */
                fos.write(tmpByteSp);

                fos.write("CNY".getBytes(getCharset()));/* 交易币种 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.payAmount.getBytes(getCharset()));/* 交易金额 */
                fos.write(tmpByteSp);

                String batBusiType = "";
                if (p_reqBat.reqHeader.isBatRec) {
                    batBusiType = getBankConfig().getSetValue("businessType", "Memo", "9188");
                    if (StringHelper.hasAnyChar(batBusiType)) {
                        batBusiType = getBankConfig().getMapValueByKey("recBusiCode", p_reqBat.readedDetail.srcXElement.getSingleText(batBusiType));
                    } else {
                        batBusiType = "D30";// 默认值:续期缴费
                    }
                } else {
                    batBusiType = getBankConfig().getSetValue("businessType", "Usage", "1988");
                    if (StringHelper.hasAnyChar(batBusiType)) {
                        batBusiType = getBankConfig().getMapValueByKey("payBusiCode", p_reqBat.readedDetail.srcXElement.getSingleText(batBusiType));
                    } else {
                        batBusiType = "C30";// 默认值:保险理赔
                    }
                }
                fos.write(batBusiType.getBytes(getCharset()));/* 收付种类 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.cellPhone.getBytes(getCharset()));/* 手机号码 */
                fos.write(tmpByteSp);

                fos.write(p_reqBat.readedDetail.authCode.getBytes(getCharset()));/* 代收协议编号 */
                fos.write(tmpByteSp);

                fos.write(tmpByteSp);/* 预留字段01 */
                fos.write(tmpByteSp);/* 预留字段02 */
                fos.write(tmpByteSp);/* 预留字段03 */
                fos.write(tmpByteSp);/* 预留字段04 */
                fos.write(tmpByteSp);/* 预留字段05 */
                fos.write(tmpByteSp);/* 预留字段06 */
                fos.write(tmpByteSp);/* 预留字段07 */
                fos.write(tmpByteSp);/* 预留字段08 */
                fos.write(tmpByteSp);/* 预留字段09 */
                /* 预留字段10 */
            }

            // 2、批量代收付文件内容写入本地并进行压缩
            WriteBankLogLn2("待压缩批次请求文件写入数据成功,当前文件内容:\r\n" + FileHelper.readFileText(tmpLocalFilePath, getCharset()));

            String tmpGZipFilePath = tmpLocalFilePath + ".gz";
            WriteBankLogLn2("压缩后批次请求文件保存本机路径:\r\n" + tmpGZipFilePath);
            WriteBankLogLn2("压缩后批次请求文件开始进行压缩...");
            GZipHelper.doCompressFile(tmpLocalFilePath, tmpGZipFilePath);
            WriteBankLogLn2("压缩后批次请求文件开始压缩完成.");

            // 3、进行文件传输
            // 2.1 报文1
            WriteBankLogLn2("压缩后批次请求文件开始进行文件传输[报文1->报文5]...");
            StringBuilder tmpSbPck1 = new StringBuilder();
            tmpSbPck1.append("I60901");/* 交易码 */
            tmpSbPck1.append("I");/* 文件创建方标识 */
            tmpSbPck1.append("B");/* 文件接收方标识 */
            tmpSbPck1.append(String.format("%-15s", corpNo));/* 保险方ID */
            tmpSbPck1.append(String.format("%-10s", "3012900"));/* 银行方ID */
            tmpSbPck1.append(String.format("%-20s", bankBranch));/* 银行分行代码 */
            tmpSbPck1.append(tmpPayDate);/* 业务日期 */
            tmpSbPck1.append(String.format("%-20s", p_reqBat.reqBatchNO));/* 交易流水号 */
            tmpSbPck1.append(tmpPayDate);/* 交易日期 */
            tmpSbPck1.append(Format.Time6Format.format(p_reqBat.getPayDateTime()));/* 交易时间 */
            tmpSbPck1.append(String.format("%-128s", tmpUploadFileName + ".gz"));/* 压缩文件名称 */
            int tmpLenGZipFile = getGZipFileLen(tmpGZipFilePath);
            WriteBankLogLn2("压缩文件字节数:" + tmpLenGZipFile);
            tmpSbPck1.append(String.format("%012d", tmpLenGZipFile));/* 文件大小 */
            int maxBlockSize = 2097152;
            int tmpBlockSize = tmpLenGZipFile;
            if (tmpBlockSize > maxBlockSize) {
                tmpBlockSize = maxBlockSize;
            }
            tmpSbPck1.append(String.format("%08d", tmpBlockSize));/* 数据包大小 */
            String tmpGZMD5 = JavaMD5.generateFileMD5(tmpGZipFilePath);
            tmpSbPck1.append(String.format("%-200s", tmpGZMD5));/* 文件MD5校验值 */

            FileInputStream fis = new FileInputStream(tmpGZipFilePath);
            boolean tmpFinished = false;
            String tmpFileInfoCode = "";
            String tmpFileMsg = "";
            int tmpStep = 1;
            int tmpReadedLen = 0;
            byte[] tmpLastBuffer = new byte[0];
            int tmpLastBufLength = 0;
            byte[] tmpNowBuffer = new byte[0];
            int tmpNowBufLength = 0;
            int tmpLastSeq = -1;
            String tmpBankAskSeq = "";// 数据包序号

            tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.filePort), this);
            WriteBankLogLn("服务地址：[" + bankFront.serverIP + ":" + bankFront.filePort + "]。");

            String[] tmpBkRet = new String[]{"", ""};
            while (tmpReadedLen <= tmpLenGZipFile) {
                if (tmpStep == 1) {
                    WriteBankLogLn("发送【报文1】请求报文体数据:\r\n" + tmpSbPck1);
                    WriteBankLogLn("开始发送【报文1】...");
                    tmpBkRet = sendToBank(tmpSbPck1.toString(), tmpClient);
                    tmpStep++;
                } else {
                    byte[] bytesBody3 = ("I60902" + tmpBankAskSeq).getBytes(getCharset());
                    byte[] bytesP3ToSend = new byte[bytesBody3.length + tmpNowBufLength];
                    System.arraycopy(bytesBody3, 0, bytesP3ToSend, 0, bytesBody3.length);
                    System.arraycopy(tmpNowBuffer, 0, bytesP3ToSend, bytesBody3.length, tmpNowBufLength);
                    WriteBankLogLn2("当前【报文3】报文体字节数:" + bytesP3ToSend.length);
                    WriteBankLogLn2("发送【报文3】请求报文体数据:\r\n" + new String(bytesP3ToSend, getCharset()));
                    WriteBankLogLn("开始发送【报文3】...");
                    tmpBkRet = sendToBank(bytesP3ToSend, tmpClient);
                    tmpLastBuffer = tmpNowBuffer;
                    tmpLastBufLength = tmpNowBufLength;
                    tmpLastSeq = Integer.parseInt(tmpBankAskSeq);
                }
                tmpBankAskSeq = "";
                tmpNowBuffer = new byte[0];
                tmpNowBufLength = 0;

                if (tmpBkRet[0].length() == 0) {
                    byte[] bytesBkRet = tmpBkRet[1].getBytes(getCharset());
                    if (bytesBkRet.length > 24) {
                        byte tmpRespType = bytesBkRet[24];
                        if (tmpRespType == 'G') {
                            WriteBankLogLn("响应报文类型为【报文2】,请求包:" + tmpBkRet[1]);
                            tmpBankAskSeq = new String(bytesBkRet, 25, 6);
                            WriteBankLogLn("【报文2】响应数据中数据包序号：" + tmpBankAskSeq);
                            int tmpAskSeq = Integer.parseInt(tmpBankAskSeq);
                            int expectedSeq = tmpLastSeq + 1;
                            if (tmpAskSeq == expectedSeq) {// 继续读取文件
                                tmpNowBuffer = new byte[maxBlockSize];
                                tmpNowBufLength = fis.read(tmpNowBuffer, 0, tmpNowBuffer.length);
                                WriteBankLogLn2("当前读取字节数:" + tmpNowBufLength);
                                if (tmpNowBufLength > 0) {
                                    tmpReadedLen += tmpNowBufLength;
                                    WriteBankLogLn2("当前实际读取字节数:" + tmpReadedLen);
                                } else {
                                    tmpFinished = false;
                                    tmpFileMsg = "数据包序号[" + tmpAskSeq + "]已无数据可读!";
                                    break;
                                }
                            } else {
                                tmpFinished = false;
                                tmpFileMsg = "数据包序号[" + tmpAskSeq + "]与预期序号[" + expectedSeq + "]不符!";
                                break;
                            }
                        } else if (tmpRespType == 'R') {
                            WriteBankLogLn("响应报文类型为【报文2】,重传包:" + tmpBkRet[1]);
                            tmpBankAskSeq = new String(bytesBkRet, 25, 6);
                            WriteBankLogLn("【报文2】请求重传数据包序号：" + tmpBankAskSeq);
                            int tmpReSendSeq = Integer.parseInt(tmpBankAskSeq);
                            if (tmpReSendSeq == tmpLastSeq) {// 继续读取文件
                                tmpNowBuffer = tmpLastBuffer;
                                tmpNowBufLength = tmpLastBufLength;
                            } else {
                                tmpFinished = false;
                                tmpFileMsg = "重传数据包序号" + tmpReSendSeq + "与上次序号" + tmpLastSeq + "不符";
                                break;
                            }
                        } else if (tmpRespType == 'N') {
                            WriteBankLogLn("响应报文类型为【报文4】:\r\n" + tmpBkRet[1]);
                            WriteBankLogLn("3.【报文4】N-文件传输成功：" + tmpBankAskSeq);
                            tmpFinished = true;
                            break;
                        } else if (tmpRespType == 'E') {
                            WriteBankLogLn("响应报文类型为【报文5】:\r\n" + tmpBkRet[1]);
                            WriteBankLogLn("3.【报文5】E-文件传输失败：" + tmpBankAskSeq);
                            tmpFinished = false;
                            tmpFileInfoCode = new String(bytesBkRet, 25, 6).trim();
                            tmpFileMsg = new String(bytesBkRet, 31, 300).trim();
                            break;
                        } else if (tmpRespType == 'A') {
                            WriteBankLogLn("响应报文类型为【报文5】:\r\n" + tmpBkRet[1]);
                            WriteBankLogLn("【报文5】A-文件传输异常：" + tmpBankAskSeq);
                            tmpFinished = false;
                            tmpFileInfoCode = new String(bytesBkRet, 25, 6).trim();
                            tmpFileMsg = new String(bytesBkRet, 31, 300).trim();
                            break;
                        } else {
                            WriteBankLogLn("无法识别的响应报文:\r\n" + tmpBkRet[1]);
                            WriteBankLogLn("3.【报文2】不能识别的应答类型：" + (char) bytesBkRet[24]);
                            tmpFinished = false;
                            tmpFileMsg = "不能识别的应答类型：" + (char) bytesBkRet[24];
                            break;
                        }
                    }
                } else {
                    tmpFinished = false;
                    tmpFileInfoCode = "E9999";
                    tmpFileMsg = tmpBkRet[1];
                    break;
                }
            }

            // 4、发起文件确认
            if (tmpFinished) {
                // 发起文件确认(B60911/I60911)
                WriteBankLogLn("文件传输成功，开始进行文件确认，连接服务：" + bankFront.serverIP + ":" + bankFront.serverPort);

                BOCOM13Header tmpBankHeader = new BOCOM13Header();
                tmpBankHeader.transCode = "I60911";
                tmpBankHeader.reqSeqID = p_reqBat.reqBatchNO;
                tmpBankHeader.corpNo = corpNo;

                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, tmpBankHeader);

                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("FileName", tmpUploadFileName + ".gz");/* 压缩文件名称 */
                xmlWriter.writeElementString("TotalNum", p_reqBat.totalNum);/* 总笔数 */
                xmlWriter.writeElementString("TotalAmt", p_reqBat.totalAmt);/* 总金额 */
                xmlWriter.writeElementString("BatNo", p_reqBat.reqBatchNO);/* 创建方批次号 */
                xmlWriter.writeEndDocument();

                String[] tmpRetStr = sendToBank(xmlWriter.toXmlString(), bankFront.serverPort);

                if (tmpRetStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retAts.respCode = "0";
                        retAts.respInfo = tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
                        String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
                        String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
                        retAts.transState = tmpMsgType + "+" + tmpErrCode;
                        retAts.payInfoCode = tmpErrCode;
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpRetStr[1];
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = tmpFileMsg;
                retAts.transState = "BatchFailed";
                retAts.payInfoCode = tmpFileInfoCode;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.toString();
            WriteBankLogLn(ex);
        } finally {
            if (tmpClient != null) {
                if (tmpClient.socket != null) {
                    tmpClient.socket.close();
                }
                if (tmpClient.tmpOutput != null) {
                    tmpClient.tmpOutput.close();
                }
                if (tmpClient.tmpInput != null) {
                    tmpClient.tmpInput.close();
                }
            }
            if (fos != null) {
                fos.close();
            }
        }

        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.reqSeqID = p_reqQryBat.reqBatchNO;

        try {
            String tmpNotifyDataPath = getNotifyDataDirectory(Format.Date8Format.format(p_reqQryBat.getBgnDateTime())) + File.separator + p_reqQryBat.reqBatchNO + ".txt";
            WriteBankLogLn("银行结果文件路径:" + tmpNotifyDataPath);

            if (new File(tmpNotifyDataPath).exists()) {
                String tmpStrRet = FileHelper.readFileText(tmpNotifyDataPath, getCharset());
                WriteBankLogLn("银行结果文件内容:\r\n" + tmpStrRet);
                StringReader strReader = new StringReader(tmpStrRet);
                BufferedReader tmpReader = new BufferedReader(strReader);
                String[] line1 = tmpReader.readLine().split("\u000F");
                if (line1 != null && line1.length > 16) {
                    retAts.respCode = "0";
                    retAts.respInfo = line1[14];
                    String tmpBatInfo = line1[4];
                    String tmpBatState = line1[2];
                    String tmpBatInfoCode = line1[3];
                    retAts.transState = tmpBatState + "+" + tmpBatInfoCode;
                    retAts.payInfoCode = tmpBatInfoCode;
                    while (true) {
                        String tmpLine = tmpReader.readLine();
                        if (StringHelper.isNullOrEmpty(tmpLine)) {
                            break;
                        }
                        String[] eachLine = tmpLine.split("\u000F");
                        if (eachLine != null && eachLine.length > 16) {
                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                            tmpRetDetail.postScript = eachLine[7];
                            if ("1998".equals(p_reqQryBat.reqHeader.transType)) {
                                tmpRetDetail.recAct = eachLine[17];
                            } else {
                                tmpRetDetail.payAct = eachLine[17];
                            }

                            tmpRetDetail.payAmount = eachLine[19];
                            String tmpResult = "";
                            String tmpBkDtState = eachLine[1];
                            String tmpBkDtInfoCode = eachLine[2];
                            String tmpBkDtInfo = eachLine[3];
                            if (tmpBkDtState.length() == 0 && tmpBkDtInfoCode.length() == 0 && tmpBkDtInfo.length() == 0 && "E".equals(tmpBkDtState) && tmpBkDtInfoCode != "000000") {
                                tmpResult = tmpBatState + "+" + tmpBatInfoCode;
                                tmpBkDtInfoCode = tmpBatInfoCode;
                                tmpBkDtInfo = tmpBatInfo;
                            } else {
                                tmpResult = "DT+" + tmpBkDtState + "+" + tmpBkDtInfoCode;
                            }
                            if (eachLine.length >= 22) {
                                tmpRetDetail.cellPhone = eachLine[21];
                                tmpRetDetail.authCode = eachLine[22];
                            }
                            tmpRetDetail.memo = tmpBkDtInfo;
                            tmpRetDetail.result = tmpResult;
                            tmpRetDetail.payInfoCode = tmpBkDtInfoCode;

                            retAts.writeRetDetail(tmpRetDetail);
                        } else {
                            break;
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "结果文件格式不符合预期，首行字段数不足17个";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "当前未收到交行的结果通知文件，请稍后查询！";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}